package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.commons.utils.Constants;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.mongo.CommentApi;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.vo.CommentVo;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.model.vo.PageResult;
import com.tanhua.server.exception.BusinessException;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class CommentService {
    @DubboReference
    private CommentApi commentApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Autowired
    private MqMessageService mqMessageService;

    public PageResult findComments(String movementId, Integer page, Integer pagesize) {
        PageResult pageResult = commentApi.findComments(movementId, page, pagesize);
        List<Comment> comments = (List<Comment>) pageResult.getItems();
        List<Long> userIds = CollUtil.getFieldValues(comments, "userId", Long.class);
        List<CommentVo> voList = new ArrayList<>();
        if (!CollUtil.isEmpty(userIds)) {
            Map<Long, UserInfo> userInfoMap = userInfoApi.findByIds(userIds);
            for (Comment comment : comments) {
                UserInfo userInfo = userInfoMap.get(comment.getUserId());
                CommentVo commentVo = CommentVo.init(userInfo, comment);
                voList.add(commentVo);
            }
        }
        pageResult.setItems(voList);
        return pageResult;
    }

    public void saveComment(String movementId, String content) {
        //检测是否冻结
        String reason = redisTemplate.opsForValue().get(Constants.FreezeComment_KEY + UserHolder.getId());
        Long restTime = redisTemplate.opsForValue().getOperations().getExpire(Constants.FreezeComment_KEY + UserHolder.getId(), TimeUnit.HOURS);
        if (reason != null) {
            throw new BusinessException("该用户处于冻结发言状态, 请联系客户处理", ErrorResult.freezeCommentError("\n 解除冻结剩余时间"+restTime+"小时"+"\n 冻结原因"+reason));
        }
        Comment comment = new Comment();
        comment.setPublishId(new ObjectId(movementId));
        comment.setCommentType(2);
        comment.setContent(content);
        comment.setUserId(UserHolder.getId());
        comment.setCreated(System.currentTimeMillis());
        comment.setLikeCount(0);
        commentApi.save(comment);
        mqMessageService.sendLogService(UserHolder.getId(),"0205","movement",movementId);
    }

    public Integer likeComment(String movementId) {
        Comment comment = new Comment();
        comment.setPublishId(new ObjectId(movementId));
        comment.setCommentType(1);
        comment.setUserId(UserHolder.getId());
        comment.setCreated(System.currentTimeMillis());
        comment.setLikeCount(0);

        String key = Constants.USER_LIKE_KEY+movementId+"_"+UserHolder.getId();
        redisTemplate.opsForValue().set(key,"1");

        mqMessageService.sendLogService(UserHolder.getId(),"0203","movement",movementId);

        return commentApi.save(comment);
    }

    public Integer dislikeComment(String movementId) {
        Comment comment = new Comment();
        comment.setPublishId(new ObjectId(movementId));
        comment.setCommentType(1);
        comment.setUserId(UserHolder.getId());

        String key = Constants.USER_LIKE_KEY+movementId+"_"+UserHolder.getId();
        redisTemplate.delete(key);

        return commentApi.delete(comment);
    }

    public Integer loveComment(String movementId) {
        Comment comment = new Comment();
        comment.setPublishId(new ObjectId(movementId));
        comment.setCommentType(3);
        comment.setUserId(UserHolder.getId());
        comment.setCreated(System.currentTimeMillis());
        comment.setLikeCount(0);

        String key = Constants.MOVEMENT_LOVE_HASHKEY+movementId+"_"+UserHolder.getId();
        redisTemplate.opsForValue().set(key,"1");

        mqMessageService.sendLogService(UserHolder.getId(),"0204","movement",movementId);

        return commentApi.save(comment);

    }

    public Integer unloveComment(String movementId) {
        Comment comment = new Comment();
        comment.setPublishId(new ObjectId(movementId));
        comment.setCommentType(3);
        comment.setUserId(UserHolder.getId());

        String key = Constants.MOVEMENT_LOVE_HASHKEY+movementId+"_"+UserHolder.getId();
        redisTemplate.delete(key);

        return commentApi.delete(comment);
    }
}
